package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/depts")
@RestController
@Slf4j
public class DeptController {
    @Autowired
    private DeptService deptService;

//新增部门
    @Log
    @PostMapping
 public Result add(@RequestBody Dept dept){
//     System.out.println("新增部门"+dept);
        log.info("新增部门:{}",dept);
     deptService.add(dept);
     return Result.success();
 }

//部门列表查询
 @GetMapping
    public Result list(String  name, Integer status,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
//     System.out.println("查询列表:"+name+","+status+","+page+","+pageSize);
     log.info("查询列表:{},{},{},{}",name,status,page,pageSize);
//调用Service分页查询
     PageResult<Dept> pageResult = deptService.list(name,status,page,pageSize);

    return Result.success(pageResult);
 }

//根据ID查询
 @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
//     System.out.println("查询id:"+id);
     log.info("查询id:{}",id);
     Dept dept = deptService.getById(id);
     return Result.success(dept);
 }

// 修改部门信息
@Log
@PutMapping
 public Result update(@RequestBody Dept dept){
//     System.out.println("修改部门信息:"+dept);
    log.info("修改部门信息:{}",dept);
     deptService.update(dept);
     return Result.success();
 }

// 删除部门
@Log
@DeleteMapping("/{id}")
 public Result deleteById(@PathVariable Integer id){
//     System.out.println("删除部门:"+id);
    log.info("删除部门:{}",id);
     deptService.deleteById(id);
     return Result.success();
 }

// 查询所有部门
 @GetMapping("/list")
    public Result select(){
//     System.out.println("查询所有部门");
     log.info("查询所有部门" );
     List<Dept> deptList = deptService.selectAll();
     return Result.success(deptList);
 }
}
